//
//  BasicVC.m
//  AccountEasy
//
//  Created by mac on 2020/7/22.
//  Copyright © 2020 AccountEasy. All rights reserved.
//

#import "BasicVC.h"
#import "UILabel+NSMutableAttributedString.h"
#define kBackGroundViewHeight 64
#define kNavItemViewHeight 44

@interface BasicVC ()

@property (nonatomic, strong ,readwrite) UIView *navItemBgView;
@property (nonatomic, strong ,readwrite) UILabel *titleLab;
@property (nonatomic, strong ,readwrite) UIButton *backBBI;
@property (nonatomic, strong) UIImageView *bgImageV; //背景图片

@end

@implementation BasicVC

#pragma mark - view controller life cycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];

    UIView *statusView = [UIView new];
    statusView.frame = SARectMake(0, 0, kBaseScreenWidth, StatusHeight - kNavItemViewHeight);
    statusView.backgroundColor = [UIColor blackColor];
    [self.view addSubview:statusView];
    self.statusView = statusView;
    
    [self initUI];
    [self initData];
}

- (UIStatusBarStyle)preferredStatusBarStyle{
//    return UIStatusBarStyleLightContent;//白色状态栏
    return UIStatusBarStyleDefault;//黑色状态栏
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:NO];
}

#pragma mark - Interface Method

- (void)setNavigationItemBackBBIAndTitle:(NSString *)title
{
    [self setTitle:title];
    [self.navItemView addSubview:self.backBBI];
}
- (void)setNavigationItemBackBBI:(NSString *)backName AndTitle:(NSString *)title
{
    [self setTitle:title];
    [self.navItemView addSubview:[self backBBIWithName:backName]];
}
- (void)setTitle:(NSString *)title
{
    [self.navItemView addSubview:self.titleLab];
    self.titleLab.text = title;
}

- (void)setTitleColor:(NSString *)title Color:(UIColor *)color Font:(UIFont *)font
{
    [self.titleLab setAttributedStringWithSubString:title color:color font:font];
}

- (void)backToLastVC
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)addRightViews:(NSArray *)views
{
    CGFloat startX = ScreenWidth;
    for (UIView *subview in views)
    {
        startX -= subview.width;
        subview.origin = CGPointMake(startX, 0);
        [self.navItemView addSubview:subview];
    }
}

- (void)addLeftViews:(NSArray *)views
{
    CGFloat startX = 0;
    
    for (UIView *subview in views)
    {
        [self.navItemView addSubview:subview];
        subview.x = startX;
        startX += subview.width;
    }
}

- (void)setBgImage:(UIImage *)image {
//    self.bgImageV.contentMode = UIViewContentModeScaleAspectFit;
//   self.bgImageV.image = [image resizableImageWithCapInsets:UIEdgeInsetsMake(image.size.height*0.9, image.size.width*0.9, image.size.height*0.9, image.size.width*0.9)resizingMode:UIImageResizingModeStretch];
    if (image) {
        self.bgImageV.image = image;
    }else {
        self.bgImageV.image = [UIImage imageNamed:@"top_image"];
    }
    self.bgImageV.hidden = NO;
    self.titleLab.textColor = [UIColor whiteColor];
    [_backBBI setImage:[UIImage imageNamed:@"whiteAllow"] forState:UIControlStateNormal];
}

- (void)initUI
{
    
}

- (void)initData
{
    
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [super touchesBegan:touches withEvent:event];
    [self.view endEditing:NO];
}

#pragma mark - lazy init

- (UIView *)navItemBgView{
    if (!_navItemBgView) {
        _navItemBgView = [[UIView alloc] init];
        _navItemBgView.backgroundColor = [UIColor whiteColor];
        _navItemBgView.frame = SARectMake(0, 0, kBaseScreenWidth, StatusHeight);
        UIImageView *bgImageV = [[UIImageView alloc] initWithFrame:SARectMake(0, 0, 375, StatusHeight)];
        _bgImageV = bgImageV;
        _bgImageV.hidden = YES;
        [_navItemBgView addSubview:bgImageV];
        [self.view addSubview:_navItemBgView];
    }
    return _navItemBgView;
}

- (UIView *)navItemView
{
    if (! _navItemView)
    {
        _navItemView = [UIView new];
        _navItemView.frame = SARectMake(0, StatusHeight - kNavItemViewHeight, kBaseScreenWidth, kNavItemViewHeight);
        [self.navItemBgView addSubview:_navItemView];
    }
    return _navItemView;
}

- (UILabel *)titleLab
{
    if (! _titleLab)
    {
        _titleLab = [UILabel new];
        _titleLab.textColor = kBlackColor;
        _titleLab.font = kFont(20);
        _titleLab.textAlignment = NSTextAlignmentCenter;
        _titleLab.frame = SARectMake(40, 0, kBaseScreenWidth - 80, kNavItemViewHeight);
    }
    return _titleLab;
}

- (UIButton *)backBBI
{
    if (! _backBBI)
    {
        _backBBI = [UIButton buttonWithType:UIButtonTypeCustom];
        [_backBBI setImage:[UIImage imageNamed:@"shop_backAllow"] forState:UIControlStateNormal];
        [_backBBI addTarget:self action:@selector(backToLastVC) forControlEvents:UIControlEventTouchUpInside];
        _backBBI.titleLabel.font = kFont(18);
        _backBBI.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        _backBBI.imageEdgeInsets = UIEdgeInsetsMake(0, 15, 0, 0);
        _backBBI.titleEdgeInsets = UIEdgeInsetsMake(0, 15, 0, 0);
        _backBBI.frame = SARectMake(0, 0, 50, kNavItemViewHeight);
    }
    return _backBBI;
}

- (UIButton *)rightTitleBtn
{
    if (! _rightTitleBtn)
    {
        _rightTitleBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        _rightTitleBtn.titleLabel.font = kFont(18);
        _rightTitleBtn.titleLabel.textColor = [UIColor whiteColor];
        _rightTitleBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
        [_navItemView addSubview:_rightTitleBtn];
        [_rightTitleBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            kMasRight(-20);kMasHeight(kNavItemViewHeight);
            kMasTop(0);
        }];
    }
    return _rightTitleBtn;
    
}

- (UIButton *)backBBIWithName:(NSString *)name
{
    UIButton  *customButton = [self backBBI];
    [customButton setTitle:name forState:UIControlStateNormal];
    return customButton;
}

- (UIButton *)backBtn
{
    return self.backBBI;
}

-(void)pushVC:(NSString *)controllerName :(BOOL)animated
{
    [self.navigationController pushViewController:(UIViewController *)[NSClassFromString(controllerName) new] animated:animated];
}

-(void)popVC:(NSString *)controllerName :(BOOL)animated
{
    NSArray *temArray = self.navigationController.viewControllers;
    BOOL pop = NO; //!<记录是否已经跳转
    for(UIViewController *temVC in temArray)
    {
        if ([temVC isKindOfClass:NSClassFromString(controllerName)])
        {
            pop = YES;
            [self.navigationController popToViewController:temVC animated:animated];
        }
    }
    
    if (!pop)
    {
        [self pushVC:controllerName :animated];
    }
}


- (void)fillBottomView
{
    if (KIsiPhoneX)
    {
        UIView *bottomView  =
        ({
            bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, kScreenHeight - kScaleNum(34), kScreenWidth, kScaleNum(34))];
            bottomView.backgroundColor = kColor(250, 250, 250, 1);
            [self.view addSubview:bottomView];
            bottomView;
        });
    }
}


- (void)removeNavItemView {
    
    [self.navItemBgView removeFromSuperview];
    [self.statusView removeFromSuperview];
}

-(void)dealloc
{
    HDLog(@"%@ 控制器已经被释放",NSStringFromClass([self class]));
}


@end
